<template>
    <section>
        <div class="order-container">
            <div class="header">
                <h4>订单入账</h4>
            </div>
            <div class="order-con">
                <el-tabs v-model="activeTab" @tab-click="handleClick" style="">
                    <el-tab-pane label="所有" name="all"></el-tab-pane>
                    <el-tab-pane label="待入账" name="stay"></el-tab-pane>
                    <el-tab-pane label="已入账" name="end"></el-tab-pane>
                </el-tabs>
                <div class="order-con__title flex-box">
                    <el-input v-model="orderName" placeholder="订单号/交易号/下单用户" style="width:300px;"></el-input>
                    <el-date-picker
                            v-model="orderTime"
                            type="datetime"
                            style="min-width:150px;"
                            placeholder="订单创建日期">
                    </el-date-picker>
                    <el-select v-model="type" placeholder="入账状态">
                        <el-option
                                v-for="item in options"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                        </el-option>
                    </el-select>
                    <el-button type="primary" @click="searchItem" icon="el-icon-search"></el-button>
                    <el-button type="primary" @click="outItem">批量导出</el-button>
                </div>
                <div class="table-box">
                    <el-table :data="orderList" v-loading="tableLoading" style="width: 100%">
                        <el-table-column :resizable="false" label="时间" prop="created_at">
                        </el-table-column>
                        <el-table-column :resizable="false" label="商品名称" prop="name">
                        </el-table-column>
                        <el-table-column :resizable="false" label="订单号/交易号">
                            <template slot-scope="scope">
                                <div>
                                    <p>{{scope.row.order_sn}}</p>
                                    <p>{{scope.row.trad_sn}}</p>
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column :resizable="false" label="下单用户">
                            <template slot-scope="scope">
                                <p>下单用户：{{scope.row.username}}</p>
                                <p>微信昵称：{{scope.row.wx_name}}</p>
                            </template>
                        </el-table-column>
                        <el-table-column :resizable="false" label="交易金额">
                            <template slot-scope="scope">
                                <span>¥{{scope.row.sale_amount}}</span>
                            </template>
                        </el-table-column>
                        <el-table-column :resizable="false" label="状态">
                            <template slot-scope="scope">
                                <!--0 交易完成 1 进行中 2 退款中 -->
                                <span v-if="scope.row.status === 0">交易完成</span>
                                <span v-else-if="scope.row.status === 1">进行中</span>
                                <span v-else-if="scope.row.status === 2">退款中</span>
                            </template>
                        </el-table-column>
                        <el-table-column :resizable="false" prop="operation" label="操作">
                            <template slot-scope="scope">
                                <el-button @click="showItem(scope.$index, scope.row.id, scope.row)" type="primary">查看详情</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
                <div class="pagination" v-if="page.total">
                    <div>
                        共{{page.count}}页，每页{{pageSize}}条
                    </div>
                    <el-pagination layout="prev, pager, next" :total="page.total" prev-text="上一页"
                                   next-text="下一页" :page-size="pageSize"
                                   @current-change="handleCurrentChange">
                    </el-pagination>
                </div>
            </div>

        </div>
    </section>
</template>

<script>
    export default {
        components: {},
        data() {
            return {
                activeTab: 'all',
                options:[
                    { label:'交易完成', value:'0'},
                    { label:'进行中', value:'1'},
                    { label:'退款中', value:'2'},
                ],

                orderTime: '',
                orderName: '',
                type:'',

                orderList: [],
                tableLoading: true,

                pageCount: '',
                page: {
                    total: null,
                    count: null
                },
                pageSize: 20,
            }
        },
        watch: {},
        computed: {},
        created(){
            var url = location.search;
            if (url.indexOf("?") != -1) {
                var str = url.substr(1);
                this.activeTab = str.split('=')[1] ;
            }
        },
        mounted() {
            this.getOrderList();
        },
        methods: {
            handleClick() {
                console.log('状态', this.activeTab)
                this.getOrderList();
            },
            genItem(){
                axios
                    .get('admin_orders', {
                        params: data
                    })
                    .then(res => {
                        this.page.total = res.data.count;
                        this.page.count = Math.ceil(res.data.count / this.pageSize);
                        return res;
                    })
                    .then(res => res.data.data)
                    .then(res => {
                        console.log('数据', res)
                        this.orderList = res;
                        this.tableLoading = false;
                    })
            },
            searchItem() {
                axios
                    .get('admin_orders', {
                        params: {
                            name: this.orderName,
                            time: this.orderTime,
                        }
                    })
                    .then(res => {
                        this.page.total = res.data.count;
                        return res;
                    })
                    .then(res => res.data.data)
                    .then(res => {
                        console.log('数据', res)
                        this.orderList = res;
                        this.tableLoading = false;
                    })
            },
            outItem() {
                console.log("批量导出")
            },
            getOrderList(pagination = 1) {
                axios
                    .get('admin_orders', {
                        params: {
                            pagination,
                            pageSize: this.pageSize,
                            type: this.activeTab === 8 ? '' : this.activeTab
                        }
                    })
                    .then(res => {
                        this.page.total = res.data.count;
                        this.page.count = Math.ceil(res.data.count / this.pageSize);
                        return res;
                    })
                    .then(res => res.data.data)
                    .then(res => {
                        console.log('数据', res)
                        this.orderList = res;
                        this.tableLoading = false;
                    })
            },
            showItem(index,id,row){
                this.$router.push({name:'OrderDetails',query:{id:id,row:row}});
            },
            handleCurrentChange(current) {
                this.getOrderList(current);
            }
        }
    }
</script>
<style scoped>
    .order-container {
        min-height: calc(100vh - 92px);
    }

    .flex-box {
        display: flex;
    }

    .header {
        padding: 10px;

        background: #FFFFFF;
    }

    .order-con {
        margin: 10px;
        padding: 20px;
        background: #FFFFFF;
    }

    .order-con__title {
        padding-bottom: 10px;
        border-bottom: 1px dashed #D8D8D8;
    }

    .table-box {
        min-height: calc(100vh - 92px);
    }

    .pagination {
        display: flex;
        align-items: center;
        justify-content: center;

        width: 100%;
    }

</style>
<style>

</style>